// pages/product/product.js
const date = new Date()
const nowYear = date.getFullYear()
const nowMonth = date.getMonth() +1
const nowDay = date.getDate()

let daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
// 根据年月 获取当月的总天数
let getDays = function (year, month) {
  if (month === 2) {
    return ((year % 4 === 0) && ((year % 100) !== 0)) || (year % 400 === 0) ? 29 : 28
  } else {
    return daysInMonth[month - 1]
  }
}
// 根据年月日设置当前月有多少天 并更新年月日数组
let setDate = function (year, month, day, _th) {
  let daysNum = year === nowYear && month === nowMonth ? nowDay : getDays(year, month)
  day = day > daysNum ? 1 : day
  let monthsNum = year === nowYear ? nowMonth : 12
  let years = []
  let months = []
  let days = []
  let yearIdx = 9999
  let monthIdx = 0
  let dayIdx = 0

  // 重新设置年份列表
  for (let i = 2016; i <= nowYear; i++) {
    years.push(i)
  }
  years.map((v, idx) => {
    if (v === year) {
      yearIdx = idx
    }
  })
  // 重新设置月份列表
  for (let i = 1; i <= monthsNum; i++) {
    months.push(i)
  }
  months.map((v, idx) => {
    if (v === month) {
      monthIdx = idx
    }
  })
  // 重新设置日期列表
  for (let i = 1; i <= daysNum; i++) {
    days.push(i)
  }
  days.map((v, idx) => {
    if (v === day) {
      dayIdx = idx
    }
  })

  _th.setData({
    years: years,//年份列表
    months: months,//月份列表
    days: days,//日期列表
    value: [yearIdx, monthIdx, dayIdx],
    year: year,
    month: month,
    day: day
  })
}
Page({
  /**
   * 页面的初始数据
   */
  data: {
    array: ['中山区', '沙河口区', '西岗区', '高新园区'],
    product:[
      {
        id:1,
        name:'教育'
      },
      {
        id: 2,
        name: '医疗'
      },
      {
        id: 3,
        name: '法律'
      },
      {
        id: 4,
        name: '生活'
      }
    ],
    nv: ['教育', '医疗', '工作', '交通', '旅行'],
    px: ['价格最低', '价格最高'],
    money:[
      {
        id: 1,
        name: '由低到高'
      },
      {
        id: 2,
        name: '由高到低'
      },
    ],
    index: 0,
    time: '08:30',
    date: '2016-09-26',
    objectArray: [
      {
        id: 1,
        name: '中山区'
      },
      {
        id: 2,
        name: '沙河口区'
      },
      {
        id: 3,
        name: '西岗区'
      },
      {
        id: 4,
        name: '高新园区'
      }
    ],
    content: [],
    nv: [],
    px: [],
    qyopen: false,
    qyshow: false,
    nzopen: false,
    pxopen: false,
    nzshow: false,
    pxshow: false,
    isfull: false,
    shijianopen:false,
    shijianshow:false,
   
    select1: '',
    select2: '',
    shownavindex: '',
    years: [],
    months: [],
    days: [],
    year: nowYear,
    month: nowMonth,
    day: nowDay,
    value: [9999, 1, 1]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    setDate(this.data.year, this.data.month, this.data.day, this)
  },
  bindChange: function (e) {
    let val = e.detail.value
    setDate(this.data.years[val[0]], this.data.months[val[1]], this.data.days[val[2]], this)
  },
  listqy: function (e) {
    if (this.data.qyopen) {
      this.setData({
        qyopen: false,
        nzopen: false,
        pxopen: false,
        nzshow: true,
        pxshow: true,
        qyshow: false,
        isfull: false,
        shownavindex: 0,
        shijianshow: false,
        shijianopen: false
      })
    } else {
      this.setData({
        qyopen: true,
        pxopen: false,
        nzopen: false,
        nzshow: true,
        pxshow: true,
        qyshow: false,
        isfull: true,
        shownavindex: e.currentTarget.dataset.nav,
        shijianshow: false,
        shijianopen: false
      })
    }

  },
  list: function (e) {
    if (this.data.nzopen) {
      this.setData({
        nzopen: false,
        pxopen: false,
        qyopen: false,
        nzshow: false,
        pxshow: true,
        qyshow: true,
        isfull: false,
        shownavindex: 0,
        shijianshow: false,
        shijianopen: false
      })
    } else {
      this.setData({
        content: this.data.nv,
        nzopen: true,
        pxopen: false,
        qyopen: false,
        nzshow: false,
        pxshow: true,
        qyshow: true,
        isfull: true,
        shownavindex: e.currentTarget.dataset.nav,
        shijianshow: false,
        shijianopen: false
      })
    }
  },
  listpx: function (e) {
    if (this.data.pxopen) {
      this.setData({
        nzopen: false,
        pxopen: false,
        qyopen: false,
        nzshow: true,
        pxshow: false,
        qyshow: true,
        isfull: false,
        shownavindex: 0,
        shijianshow: false,
        shijianopen: false
      })
    } else {
      this.setData({
        content: this.data.px,
        nzopen: false,
        pxopen: true,
        qyopen: false,
        nzshow: true,
        pxshow: false,
        qyshow: true,
        isfull: true,
        shownavindex: e.currentTarget.dataset.nav,
        shijianshow: false,
        shijianopen: false
      })
    }
    console.log(e.target)
  },
  shijianpx: function (e) {
    if (this.data.shijianopen) {
      this.setData({
        nzopen: false,
        pxopen: false,
        qyopen: false,
        nzshow: false,
        pxshow: false,
        qyshow: false,
        isfull: false,
        shijianshow:false,
        shijianopen: false
      })
    } else {
      this.setData({
        content: this.data.px,
        nzopen: false,
        pxopen: false,
        qyopen: false,
        nzshow: false,
        pxshow: false,
        qyshow: false,
        isfull: true,
        shijianshow: false,
        shijianopen: true
      })
    }
    console.log(e.target)
  },
  selectleft: function (e) {

    this.setData({
      cityright: {},
      citycenter: this.data.cityleft[e.currentTarget.dataset.city],
      select1: e.target.dataset.city,
      select2: ''
    });
  },
  selectcenter: function (e) {

    this.setData({
      cityright: this.data.citycenter[e.currentTarget.dataset.city],
      select2: e.target.dataset.city
    });
  },
  hidebg: function (e) {

    this.setData({
      qyopen: false,
      nzopen: false,
      pxopen: false,
      nzshow: true,
      pxshow: true,
      qyshow: true,
      isfull: false,
      shownavindex: 0
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      contentlist: [
        {
          title: '高新园区幼儿园名额',
          att: '教',
          address: '高新园区',
          pre_time: '1小时前',
          bet_time: '2018-3-28至2018-4-10',
          money: '200',
          img: '../image/vol.jpg'
        },
        {
          title: '中山区幼儿园名额',
          att: '教',
          address: '高新园区',
          pre_time: '1小时前',
          bet_time: '2018-3-28至2018-4-10',
          money: '300',
          img: '../image/vol.jpg'
        },
        {
          title: '西岗区幼儿园名额',
          att: '教',
          address: '高新园区',
          pre_time: '1小时前',
          bet_time: '2018-3-28至2018-4-10',
          money: '400',
          img: '../image/vol.jpg'
        }
      ]
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})